#include "FormModel.h"
#include <QLineEdit>
#include <QTextEdit>
#include <QPlainTextEdit>
#include <QDateTimeEdit>
#include <QComboBox>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QTableWidgetItem>
#include <QAbstractButton>

#include "Table.h"

using namespace Qy;

FormModel::FormModel(Table* table, QObject *parent) : QObject(parent), table(table)
{
    if(table) table->setParent(this);
}

void FormModel::connect(QWidget *inputWidget, const QString &fieldName, FormModelType type, const QString& QDateTimeForceStringFormat)
{
    if(hashInputWidget.contains(fieldName)) return;
    listFieldName.append(fieldName);
    hashInputWidget.insert(fieldName, inputWidget);
    inputWidget->setProperty("FormModelType", type);
    if(!QDateTimeForceStringFormat.isEmpty()) inputWidget->setProperty("ForceString", QDateTimeForceStringFormat);
}

void FormModel::unconnect()
{
    hashInputWidget.clear();
    listFieldName.clear();
}

void FormModel::connect(QTableWidget *table, QTableWidgetItem *item, const QString &fieldName)
{
    if(hashTableItem.contains(fieldName)) return;
    connect(table, fieldName, QTableWidgetItemType);
    hashTableItem.insert(fieldName, item);
}

void FormModel::connect(QTableWidget *table, QTableWidgetItem *item, Field &field)
{
    connect(table, item, field.fieldName);
}

void FormModel::connectNewTableItem(QTableWidget *table, int row, const QStringList &fieldNames,
                                    int columnStart, QList<int> customColumn)
{
    if(customColumn.size() && customColumn.size() != fieldNames.size()) return;
    for(int i = 0; i < fieldNames.size(); ++i){
        int col = (customColumn.size() ? customColumn.at(i) : columnStart + i);
        QTableWidgetItem* item = new QTableWidgetItem;
        table->setItem(row, col, item);
        connect(table, item, fieldNames.at(i));
    }
}

void FormModel::getData(const QString &fieldName, bool directChange)
{
    QWidget *inputWidget = hashInputWidget.value(fieldName);
    switch (inputWidget->property("FormModelType").toInt()) {
    case QLineEditType: {
        QVariant data;
        QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(inputWidget);
        QString stringData = lineEdit->text();
        if(!stringData.isEmpty()){
            if(lineEdit->inputMask().isEmpty() || stringData.size() == lineEdit->displayText().size()){
                data = stringData;
            }
        }
        if(hashGetFilter.contains(QLineEditType)){
            data = hashGetFilter.value(QLineEditType)(data, inputWidget);
        }
        if(directChange || data != table->getFieldValue(fieldName)){
            table->setFieldValue(fieldName, data);
        }
    }break;
    case QTextEditType: {
        QVariant data;
        QString stringData = dynamic_cast<QTextEdit*>(inputWidget)->toPlainText();
        if(!stringData.isEmpty()){
            data = stringData;
        }
        if(hashGetFilter.contains(QTextEditType)){
            data = hashGetFilter.value(QTextEditType)(data, inputWidget);
        }
        if(directChange || data != table->getFieldValue(fieldName)){
            table->setFieldValue(fieldName, data);
        }
    }break;
    case QPlainTextEditType: {
        QVariant data;
        QString stringData = dynamic_cast<QPlainTextEdit*>(inputWidget)->toPlainText();
        if(!stringData.isEmpty()){
            data = stringData;
        }
        if(hashGetFilter.contains(QPlainTextEditType)){
            data = hashGetFilter.value(QPlainTextEditType)(data, inputWidget);
        }
        if(directChange || data != table->getFieldValue(fieldName)){
            table->setFieldValue(fieldName, data);
        }
    }break;
    case QDateTimeEditType: {
        QString QDateTimeForceStringFormat = inputWidget->property("ForceString").toString();
        QDateTimeEdit* dateTimeEdit = dynamic_cast<QDateTimeEdit*>(inputWidget);
        QDateTime data = dateTimeEdit->dateTime();
        if(hashGetFilter.contains(QDateTimeEditType)){
            data = hashGetFilter.value(QDateTimeEditType)(data, inputWidget).toDateTime();
        }
        if(QDateTimeForceStringFormat.isEmpty()){
            if(directChange || dateTimeEdit->dateTime() != table->getFieldValue(fieldName).toDateTime()){
                table->setFieldValue(fieldName, data);
            }
        }else{
            QString tdata = (data == dateTimeEdit->minimumDateTime() ? "" :
                            data.toString(QDateTimeForceStringFormat));
            if(directChange || tdata != table->getFieldValue(fieldName).toString()){
                table->setFieldValue(fieldName, tdata);
            }
        }
    }break;
    case QComboBoxType: {
        QVariant data;
        if(dynamic_cast<QComboBox*>(inputWidget)->currentIndex() >= 0){
            data = dynamic_cast<QComboBox*>(inputWidget)->currentText();
        }
        if(hashGetFilter.contains(QComboBoxType)){
            data = hashGetFilter.value(QComboBoxType)(data, inputWidget);
        }
        if(data.type() == QVariant::UserType) break;
        if(directChange || data != table->getFieldValue(fieldName)){
            table->setFieldValue(fieldName, data);
        }
    }break;
    case QComboBoxDataType: {
        QVariant data;
        if(dynamic_cast<QComboBox*>(inputWidget)->currentIndex() >= 0){
            data = dynamic_cast<QComboBox*>(inputWidget)->currentText();
        }
        if(hashGetFilter.contains(QComboBoxDataType)){
            data = hashGetFilter.value(QComboBoxDataType)(data, inputWidget);
        }
        if(data.type() == QVariant::UserType) break;
        if(directChange || data != table->getFieldValue(fieldName)){
            table->setFieldValue(fieldName, data);
        }
    }break;
    case QSpinBoxType: {
        QVariant data = dynamic_cast<QSpinBox*>(inputWidget)->value();
        if(hashGetFilter.contains(QSpinBoxType)){
            data = hashGetFilter.value(QSpinBoxType)(data, inputWidget);
        }
        if(directChange || data != table->getFieldValue(fieldName)){
            table->setFieldValue(fieldName, data);
        }
    }break;
    case QDoubleSpinBoxType: {
        QVariant data = dynamic_cast<QDoubleSpinBox*>(inputWidget)->value();
        if(hashGetFilter.contains(QDoubleSpinBoxType)){
            data = hashGetFilter.value(QDoubleSpinBoxType)(data, inputWidget).toDouble();
        }
        QVariant old = table->getFieldValue(fieldName);
        if(directChange || (data.isValid() != old.isValid() || (data.isValid() && !qFuzzyCompare(data.toDouble(), old.toDouble())))){
            table->setFieldValue(fieldName, data);
        }
    }break;
    case QAbstractButtonType: {
        QVariant data = dynamic_cast<QAbstractButton*>(inputWidget)->isChecked();
        if(hashGetFilter.contains(QAbstractButtonType)){
            data = hashGetFilter.value(QAbstractButtonType)(data, inputWidget);
        }
        if(directChange || data != table->getFieldValue(fieldName)){
            table->setFieldValue(fieldName, data);
        }
    }break;
    case BlobType: {
        QVariant data = dynamic_cast<BlobAbstractModel*>(inputWidget)->blob;
        if(hashGetFilter.contains(BlobType)){
            data = hashGetFilter.value(BlobType)(data, inputWidget);
        }
        if(directChange || dynamic_cast<BlobAbstractModel*>(inputWidget)->isChanged){
            table->setFieldValue(fieldName, data);
        }
    }break;
    case QTableWidgetItemType: {
        QTableWidgetItem* item = hashTableItem.value(fieldName);
        QVariant data;
        QString stringData = item->text();
        if(!stringData.isEmpty()){
            data = stringData;
        }
        if(hashGetFilter.contains(QTableWidgetItemType)){
            data = hashGetFilter.value(QTableWidgetItemType)(data, reinterpret_cast<QWidget*>(item));
        }
        if(directChange || data != table->getFieldValue(fieldName)){
            table->setFieldValue(fieldName, data);
        }
    }break;
    default:break;
    }
}

void FormModel::getData()
{
    for(const QString& fieldName : listFieldName){
        getData(fieldName, true);
    }
}

void FormModel::getChangedData()
{
    for(const QString& fieldName : listFieldName){
        getData(fieldName, false);
    }
}

void FormModel::setData(const QString &fieldName, QWidget *inputWidget)
{
    switch (inputWidget->property("FormModelType").toInt()) {
    case QLineEditType: {
        QVariant data = table->getFieldValue(fieldName);
        if(hashSetFilter.contains(QLineEditType)){
            data = hashSetFilter.value(QLineEditType)(data, inputWidget);
        }
        dynamic_cast<QLineEdit*>(inputWidget)->setText(data.toString());
    }break;
    case QTextEditType: {
        QVariant data = table->getFieldValue(fieldName);
        if(hashSetFilter.contains(QTextEditType)){
            data = hashSetFilter.value(QTextEditType)(data, inputWidget);
        }
        dynamic_cast<QTextEdit*>(inputWidget)->setText(data.toString());
    }break;
    case QPlainTextEditType: {
        QVariant data = table->getFieldValue(fieldName);
        if(hashSetFilter.contains(QPlainTextEditType)){
            data = hashSetFilter.value(QPlainTextEditType)(data, inputWidget);
        }
        dynamic_cast<QPlainTextEdit*>(inputWidget)->setPlainText(data.toString());
    }break;
    case QDateTimeEditType: {
        QString QDateTimeForceStringFormat = inputWidget->property("ForceString").toString();
        QDateTimeEdit* dateTimeEdit = dynamic_cast<QDateTimeEdit*>(inputWidget);
        if(QDateTimeForceStringFormat.isEmpty()){
            QVariant data = table->getFieldValue(fieldName);
            if(hashSetFilter.contains(QDateTimeEditType)){
                data = hashSetFilter.value(QDateTimeEditType)(data, inputWidget);
            }
            dateTimeEdit->setDateTime(data.toDateTime());
        }else{
            QString dateTimeString = table->getFieldValue(fieldName).toString();
            if(dateTimeString.isEmpty()){
                dateTimeEdit->setDateTime(dateTimeEdit->minimumDateTime());
            }else{
                QDateTime dateTime = QDateTime::fromString(dateTimeString, QDateTimeForceStringFormat);
                if(hashSetFilter.contains(QDateTimeEditType)){
                    dateTime = hashSetFilter.value(QDateTimeEditType)(dateTime, inputWidget).toDateTime();
                }
                dateTimeEdit->setDateTime(dateTime);
            }
        }
    }break;
    case QComboBoxType: {
        QVariant data = table->getFieldValue(fieldName);
        if(hashSetFilter.contains(QComboBoxType)){
            data = hashSetFilter.value(QComboBoxType)(data, inputWidget);
        }
        if(data.isNull()) dynamic_cast<QComboBox*>(inputWidget)->setCurrentIndex(-1);
        else dynamic_cast<QComboBox*>(inputWidget)->setCurrentText(data.toString());
    }break;
    case QComboBoxDataType: {
        QVariant data = table->getFieldValue(fieldName);
        if(hashSetFilter.contains(QComboBoxDataType)){
            data = hashSetFilter.value(QComboBoxDataType)(data, inputWidget);
        }
        if(data.isNull()) dynamic_cast<QComboBox*>(inputWidget)->setCurrentIndex(-1);
        else dynamic_cast<QComboBox*>(inputWidget)->setCurrentText(data.toString());
    }break;
    case QSpinBoxType: {
        QVariant data = table->getFieldValue(fieldName);
        if(hashSetFilter.contains(QSpinBoxType)){
            data = hashSetFilter.value(QSpinBoxType)(data, inputWidget);
        }
        dynamic_cast<QSpinBox*>(inputWidget)->setValue(data.toInt());
    }break;
    case QDoubleSpinBoxType: {
        QVariant data = table->getFieldValue(fieldName);
        if(hashSetFilter.contains(QDoubleSpinBoxType)){
            data = hashSetFilter.value(QDoubleSpinBoxType)(data, inputWidget);
        }
        dynamic_cast<QDoubleSpinBox*>(inputWidget)->setValue(data.toDouble());
    }break;
    case QAbstractButtonType: {
        QVariant data = table->getFieldValue(fieldName);
        if(hashSetFilter.contains(QAbstractButtonType)){
            data = hashSetFilter.value(QAbstractButtonType)(data, inputWidget);
        }
        dynamic_cast<QAbstractButton*>(inputWidget)->setChecked(data.toBool());
    }break;
    case BlobType: {
        QVariant data = table->getFieldValue(fieldName);
        if(hashSetFilter.contains(BlobType)){
            data = hashSetFilter.value(BlobType)(data, inputWidget);
        }
        dynamic_cast<BlobAbstractModel*>(inputWidget)->blob = data.toByteArray();
    }break;
    case QTableWidgetItemType: {
        QTableWidgetItem* item = hashTableItem.value(fieldName);
        QVariant data = table->getFieldValue(fieldName);
        if(hashSetFilter.contains(QTableWidgetItemType)){
            data = hashSetFilter.value(QTableWidgetItemType)(data, reinterpret_cast<QWidget*>(item));
        }
        item->setText(data.toString());
    }break;
    default:break;
    }
}

void FormModel::setData(const QString &fieldName)
{
    setData(fieldName, hashInputWidget.value(fieldName));
}

void FormModel::setData()
{
    for(const QString& fieldName : listFieldName){
        QWidget *inputWidget = hashInputWidget.value(fieldName);
        setData(fieldName, inputWidget);
    }
}

void FormModel::clearData(const QString &fieldName, QWidget *inputWidget)
{
    switch (inputWidget->property("FormModelType").toInt()) {
    case QLineEditType: {
        dynamic_cast<QLineEdit*>(inputWidget)->setText(QString());
    }break;
    case QTextEditType: {
        dynamic_cast<QTextEdit*>(inputWidget)->setText(QString());
    }break;
    case QPlainTextEditType: {
        dynamic_cast<QPlainTextEdit*>(inputWidget)->setPlainText(QString());
    }break;
    case QDateTimeEditType: {
        dynamic_cast<QDateTimeEdit*>(inputWidget)->setDateTime(QDateTime());
    }break;
    case QComboBoxType: {
        dynamic_cast<QComboBox*>(inputWidget)->setCurrentIndex(-1);
    }break;
    case QComboBoxDataType: {
        dynamic_cast<QComboBox*>(inputWidget)->setCurrentIndex(-1);
    }break;
    case QSpinBoxType: {
        dynamic_cast<QSpinBox*>(inputWidget)->setValue(0);
    }break;
    case QDoubleSpinBoxType: {
        dynamic_cast<QDoubleSpinBox*>(inputWidget)->setValue(0.0);
    }break;
    case QAbstractButtonType: {
        dynamic_cast<QAbstractButton*>(inputWidget)->setChecked(false);
    }break;
    case BlobType: {
    }break;
    case QTableWidgetItemType: {
        hashTableItem.value(fieldName)->setText(QString());
    }break;
    default:break;
    }
}

void FormModel::clearData(const QString &fieldName)
{
    clearData(fieldName, hashInputWidget.value(fieldName));
}

void FormModel::clearData()
{
    for(const QString& fieldName : listFieldName){
        QWidget *inputWidget = hashInputWidget.value(fieldName);
        clearData(fieldName, inputWidget);
    }
}

void FormModel::clearChanged()
{
    table->clearChanged();
}

bool FormModel::hasChange()
{
    return table->fieldChanged.size();
}

void FormModel::setReadOnly(bool isReadOnly)
{
    for(const QString& fieldName : listFieldName){
        QWidget *inputWidget = hashInputWidget.value(fieldName);
        switch (inputWidget->property("FormModelType").toInt()) {
        case QLineEditType: {
            dynamic_cast<QLineEdit*>(inputWidget)->setReadOnly(isReadOnly);
        }break;
        case QTextEditType: {
            dynamic_cast<QTextEdit*>(inputWidget)->setReadOnly(isReadOnly);
        }break;
        case QPlainTextEditType: {
            dynamic_cast<QPlainTextEdit*>(inputWidget)->setReadOnly(isReadOnly);
        }break;
        case QDateTimeEditType: {
            dynamic_cast<QDateTimeEdit*>(inputWidget)->setReadOnly(isReadOnly);
        }break;
        case QComboBoxType: {
            dynamic_cast<QComboBox*>(inputWidget)->setEnabled(isReadOnly);
        }break;
        case QComboBoxDataType: {
            dynamic_cast<QComboBox*>(inputWidget)->setEnabled(isReadOnly);
        }break;
        case QSpinBoxType: {
            dynamic_cast<QSpinBox*>(inputWidget)->setReadOnly(isReadOnly);
        }break;
        case QDoubleSpinBoxType: {
            dynamic_cast<QDoubleSpinBox*>(inputWidget)->setReadOnly(isReadOnly);
        }break;
        case QAbstractButtonType: {
            dynamic_cast<QAbstractButton*>(inputWidget)->setCheckable(isReadOnly);
        }break;
        case BlobType: {
            dynamic_cast<BlobAbstractModel*>(inputWidget)->isReadonly = isReadOnly;
        }break;
        case QTableWidgetItemType: {
            QTableWidgetItem* item = hashTableItem.value(fieldName);
            Qt::ItemFlags flags = item->flags();
            flags.setFlag(Qt::ItemIsEditable, isReadOnly);
            item->setFlags(flags);
        }break;
        default:break;
        }
    }
}

void FormModel::setEnabled(bool isEnabled)
{
    for(const QString& fieldName : listFieldName){
        QWidget *inputWidget = hashInputWidget.value(fieldName);
        switch (inputWidget->property("FormModelType").toInt()) {
        case QLineEditType: {
            dynamic_cast<QLineEdit*>(inputWidget)->setEnabled(isEnabled);
        }break;
        case QTextEditType: {
            dynamic_cast<QTextEdit*>(inputWidget)->setEnabled(isEnabled);
        }break;
        case QPlainTextEditType: {
            dynamic_cast<QPlainTextEdit*>(inputWidget)->setEnabled(isEnabled);
        }break;
        case QDateTimeEditType: {
            dynamic_cast<QDateTimeEdit*>(inputWidget)->setEnabled(isEnabled);
        }break;
        case QComboBoxType: {
            dynamic_cast<QComboBox*>(inputWidget)->setEnabled(isEnabled);
        }break;
        case QComboBoxDataType: {
            dynamic_cast<QComboBox*>(inputWidget)->setEnabled(isEnabled);
        }break;
        case QSpinBoxType: {
            dynamic_cast<QSpinBox*>(inputWidget)->setEnabled(isEnabled);
        }break;
        case QDoubleSpinBoxType: {
            dynamic_cast<QDoubleSpinBox*>(inputWidget)->setEnabled(isEnabled);
        }break;
        case QAbstractButtonType: {
            dynamic_cast<QAbstractButton*>(inputWidget)->setEnabled(isEnabled);
        }break;
        case BlobType: {
            dynamic_cast<BlobAbstractModel*>(inputWidget)->isReadonly = !isEnabled;
        }break;
        case QTableWidgetItemType: {
            QTableWidgetItem* item = hashTableItem.value(fieldName);
            Qt::ItemFlags flags = item->flags();
            if(isEnabled) flags |= Qt::ItemIsEditable;
            else {
                if(flags & Qt::ItemIsEditable) flags ^= Qt::ItemIsEditable;
            }
            item->setFlags(flags);
        }break;
        default:break;
        }
    }
}

void FormModel::connectChangedSignal(std::function<void()> _lambda)
{
    for(const QString& fieldName : listFieldName){
        QWidget *inputWidget = hashInputWidget.value(fieldName);
        switch (inputWidget->property("FormModelType").toInt()) {
        case QLineEditType: {
            QObject::connect(dynamic_cast<QLineEdit*>(inputWidget), &QLineEdit::textChanged, _lambda);
        }break;
        case QTextEditType: {
            QObject::connect(dynamic_cast<QTextEdit*>(inputWidget), &QTextEdit::textChanged, _lambda);
        }break;
        case QPlainTextEditType: {
            QObject::connect(dynamic_cast<QPlainTextEdit*>(inputWidget), &QPlainTextEdit::textChanged, _lambda);
        }break;
        case QDateTimeEditType: {
            QObject::connect(dynamic_cast<QDateTimeEdit*>(inputWidget), &QDateTimeEdit::dateTimeChanged, _lambda);
        }break;
        case QComboBoxType: {
            QObject::connect(dynamic_cast<QComboBox*>(inputWidget), &QComboBox::currentTextChanged, _lambda);
        }break;
        case QComboBoxDataType: {
            QObject::connect(dynamic_cast<QComboBox*>(inputWidget), &QComboBox::currentTextChanged, _lambda);
        }break;
        case QSpinBoxType: {
            QObject::connect(dynamic_cast<QSpinBox*>(inputWidget), QOverload<int>::of(&QSpinBox::valueChanged), _lambda);
        }break;
        case QDoubleSpinBoxType: {
            QObject::connect(dynamic_cast<QDoubleSpinBox*>(inputWidget), QOverload<double>::of(&QDoubleSpinBox::valueChanged), _lambda);
        }break;
        case QAbstractButtonType: {
            QObject::connect(dynamic_cast<QAbstractButton*>(inputWidget), &QAbstractButton::toggled, _lambda);
        }break;
        case BlobType: {
            dynamic_cast<BlobAbstractModel*>(inputWidget)->changedLambda = _lambda;
        }break;
        default:break;
        }
    }
}

#define CONNECTWIDGETFIELDNAME(T) void FormModel::connect(T *inputWidget, const QString &fieldName)\
{connect(inputWidget, fieldName, T##Type);}
CONNECTWIDGETFIELDNAME(QLineEdit)
CONNECTWIDGETFIELDNAME(QTextEdit)
CONNECTWIDGETFIELDNAME(QPlainTextEdit)
CONNECTWIDGETFIELDNAME(QDateTimeEdit)
CONNECTWIDGETFIELDNAME(QComboBox)
CONNECTWIDGETFIELDNAME(QSpinBox)
CONNECTWIDGETFIELDNAME(QDoubleSpinBox)
CONNECTWIDGETFIELDNAME(QAbstractButton)
void FormModel::connect(BlobAbstractModel *inputWidget, const QString &fieldName)
{
    connect(reinterpret_cast<QWidget*>(inputWidget), fieldName, BlobType);
}

#define CONNECTWIDGETFIELD(T) void FormModel::connect(T *inputWidget, Field& field)\
{connect(inputWidget, field.fieldName, T##Type);}
CONNECTWIDGETFIELD(QLineEdit)
CONNECTWIDGETFIELD(QTextEdit)
CONNECTWIDGETFIELD(QPlainTextEdit)
CONNECTWIDGETFIELD(QDateTimeEdit)
CONNECTWIDGETFIELD(QComboBox)
CONNECTWIDGETFIELD(QSpinBox)
CONNECTWIDGETFIELD(QDoubleSpinBox)
CONNECTWIDGETFIELD(QAbstractButton)
void FormModel::connect(BlobAbstractModel *inputWidget, Field& field)
{
    connect(reinterpret_cast<QWidget*>(inputWidget), field.fieldName, BlobType);
}

void FormModel::clearView()
{
    listFieldNameView.clear();
    hashInputWidgetView.clear();
    hashTableItemView.clear();
}

void FormModel::setViewData()
{
    for(const QString& fieldName : listFieldNameView){
        for(QWidget *inputWidget : hashInputWidgetView.value(fieldName)){
            FormModelType type = FormModelType(inputWidget->property("FormModelType").toInt());
            if(type == QTableWidgetItemType){
                QString data = table->getFieldValue(fieldName).toString();
                for(QTableWidgetItem* item : hashTableItemView.value(fieldName)){
                    if(item){
                        if(hashSetFilter.contains(QTableWidgetItemType)){
                            data = hashSetFilter.value(QTableWidgetItemType)(data, reinterpret_cast<QWidget*>(item)).toString();
                        }
                        item->setText(data);
                    }else{
                        hashTableItemView[fieldName].removeOne(item);
                    }
                }
            }else{
                if(inputWidget){
                    setData(fieldName, inputWidget);
                }else{
                    hashInputWidgetView[fieldName].removeOne(inputWidget);
                }
            }
        }
    }
}

void FormModel::connectView(QWidget *inputWidget, const QString &fieldName, FormModelType type, const QString &QDateTimeForceStringFormat)
{
    if(!hashInputWidgetView.contains(fieldName)){
        listFieldNameView.append(fieldName);
        hashInputWidgetView.insert(fieldName, QList<QWidget*>());
    }
    hashInputWidgetView[fieldName].append(inputWidget);

    inputWidget->setProperty("FormModelType", type);
    if(!QDateTimeForceStringFormat.isEmpty()) inputWidget->setProperty("ForceString", QDateTimeForceStringFormat);
}

void FormModel::connectView(QTableWidget *table, QTableWidgetItem *item, const QString &fieldName)
{
    if(!hashTableItemView.contains(fieldName)){
        hashTableItemView.insert(fieldName, QList<QTableWidgetItem*>());
        listFieldNameView.append(fieldName);
        hashInputWidgetView.insert(fieldName, QList<QWidget*>());
        table->setProperty("FormModelType", QTableWidgetItemType);
    }
    hashTableItemView[fieldName].append(item);
    hashInputWidgetView[fieldName].append(table);
}
